﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LANChatCore
{
    public class RoomManager
    {
        private readonly List<Room> _rooms;

        public RoomManager()
        {
            _rooms = new List<Room>();    
        }

        /// <summary>
        /// Creates a new room
        /// </summary>
        public bool OpenRoom(string roomName)
        {
            if (roomName == null)
                throw new ArgumentNullException(Utilities.GetVariableName(roomName));

            if (_rooms == null)
                return false;

            _rooms.Add(new Room(roomName));

            return true;
        }

        /// <summary>
        /// Closes a open room
        /// </summary>
        /// <param name="roomName">Name of the room to close</param>
        /// <returns>True of the room closed, false otherwise</returns>
        public bool CloseRoom(String roomName)
        {
            if (String.IsNullOrEmpty(roomName))
                throw new ArgumentException(ErrorStrings.ExMsgUserNameNullOrEmpty, Utilities.GetVariableName(roomName));


            if (_rooms == null)
                return false;

            Room foundRoom = FindRoom(roomName);

            // Remove a room
            if (!_rooms.Remove(foundRoom))
                return false;

            return true;
        }

        /// <summary>
        /// Find a room by name.
        /// </summary>
        /// <param name="roomName">The name of the room to find.</param>
        /// <returns>A valid room if found</returns>
        public Room FindRoom(string roomName)
        {
            if (String.IsNullOrEmpty(roomName))
                throw new ArgumentException(ErrorStrings.ExMsgUserNameNullOrEmpty, Utilities.GetVariableName(roomName));

            return _rooms.Find(r => r.Name == roomName);
        }
    }
}
